package com.FTW.SAA.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.rag.retrieval.search.VectorStoreDocumentRetriever;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequestMapping("/rag")
@RestController
public class RagController {

    @Resource
    private VectorStore vectorStore;

    @Resource
    @Qualifier("deepSeekClient")
    private ChatClient deepSeekClient;


    @GetMapping("/rt")
    public Flux<String> retrieve(@RequestParam(value = "userCode") String userCode) {
        String systemPrompt = """
        你是一名玩家身份识别助手，能够根据用户提供的身份码告知用户身份，如果没有检索到提供的身份码则告知身份未知
        """;

        RetrievalAugmentationAdvisor advisor = RetrievalAugmentationAdvisor.builder()   //检索增强，告知AI需要从知识库中检索数据
                .documentRetriever(VectorStoreDocumentRetriever.builder()
                        .vectorStore(vectorStore).build())  //指定向量数据库
                .build();

        return deepSeekClient.prompt()
                .system(systemPrompt)
                .user(userCode)
                .advisors(advisor)
                .stream()
                .content();
    }
}
